package com.joymart.carousel.rest;

import com.joymart.common.error.handling.JoymartBusinessException;
import com.joymart.common.utils.SecurityUtils;
import com.joymart.dictionary.Dictionary;
import com.joymart.dictionary.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;

@RestController
public class CarouselController {

    @Autowired
    private DictionaryService dictionaryService;

    @GetMapping("/carousel/items")
    public Object getDictionary(){
        Dictionary dict = dictionaryService.findByName("carousel_items");
        return dict.getKeyValues();
    }

    /**
     * 维护字典项
     * @param name
     * @return
     */
    @PutMapping("/admin/carousel/items")
    public void maintainDictionary( @RequestBody LinkedHashMap<String, Dictionary.Value> items){
        SecurityUtils.requiresRestAuthority("ProductRelated");
        Dictionary dict = new Dictionary();
        dict.setName("carousel_items");
        dict.setKeyValues(items);
        dictionaryService.maintainDictionary(dict);
    }

}
